package leetcode.code0870;

import java.util.Arrays;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int[] advantageCount(int[] nums1, int[] nums2) {
		int len = nums1.length;
		Arrays.sort(nums1);
		int[][] n2 = new int[len][2];
		for (int i = 0; i < len; i++) {
			n2[i][0] = nums2[i];
			n2[i][1] = i;
		}
		Arrays.sort(n2, (a, b) -> a[0] - b[0]);
		int[] ans = new int[len];
		Arrays.fill(ans, -1);
		int p1 = 0, p2 = 0;
		while (p2 < len && p1 < len) {
			int cur = n2[p2][0];
			while (p1 < len && nums1[p1] <= cur) {
				p1++;
			}
			if (n2[p2][1] < len && p1 < len) {
				ans[n2[p2][1]] = nums1[p1];
				nums1[p1] = -1;
			}
			p1++;
			p2++;
		}
		p1 = 0;
		p2 = 0;
		while (p2 < len) {
			if (ans[p2] == -1) {
				while (nums1[p1] == -1) {
					p1++;
				}
				ans[p2] = nums1[p1++];
			}
			p2++;
		}
		return ans;
	}

	@Override
	public void debug4() {
		this.advantageCount(H.str2array3("[2,0,4,1,2]"), H.str2array3("[1,3,0,0,2]"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
